﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.Areas.BizProj.Models;
using ProjBackEnd.Lib;
using ProjBackEnd.DAL;

namespace ProjBackEnd.Areas.BizProj.Controllers
{
    /// <summary>
    /// 项目申请单操作
    /// </summary>
    public class ProjApplyOptController : ApiController
    {
        /// <summary>
        /// 从项目维度获取项目申请单列表，项目编号和申请单编号不能同时为 0
        /// eg. : 0000000001|TST1|1|0|0000000001
        /// </summary>
        /// <param name="key">memberid|comcd|项目编号（0=所有）|申请单编号（0=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/projapplyopt")]
        public RespProjApply GetProjApply(string key)
        {
            RespProjApply resp = new RespProjApply();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            int iProjid, iApplyid;
            if (!int.TryParse(strsKeyOriginal[2], out iProjid) || !int.TryParse(strsKeyOriginal[3], out iApplyid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (iProjid == 0 && iApplyid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "项目编号和申请单编号不能同时为 0";
                return resp;
            }

            resp = projLogic.GetProjApply(iProjid, iApplyid);
            return resp;
        }

        /// <summary>
        /// 项目申请单常规信息维护
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/projapplyopt")]
        public RespProjNullResultInfo UpdateProjApply(ReqProjApply _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateProjApply(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.projapplyinfo,_reqInfo.projdtlcorelist);
            return resp;
        }

        /// <summary>
        /// 项目申请单敏感信息维护
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/projapplyoptsens")]
        public RespProjNullResultInfo UpdateProjApplySens(ReqProjApplySens _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            //resp = projLogic.UpdateProjApply(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.projapplyinfo, _reqInfo.projdtlcorelist);
            return resp;
        }

    }
}
